﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DatabaseAccess;
using System.Data;

namespace Liguo.Administration
{
    public partial class SendGoods : System.Web.UI.Page
    {
        private DataSet ds = new DataSet();
        private SqlIt sql = new SqlIt();
        private ArrayList orderids=new ArrayList();
        private ArrayList delivery = new ArrayList();

        protected void Page_Load(object sender, EventArgs e)
        {
            orderids = (ArrayList)Session["SendGoodsID"];
            delivery = (ArrayList)Session["SendGoodsDel"];
            if (orderids==null)
            {
                Response.Write("<script>alert('无货可送')</script>");
                Response.Redirect("/Administration/UnfinishedOrders.aspx");
            }
            ExecuteStorage(orderids);
            if (!IsPostBack)
            {
                bind();
                orderids.Clear();
                delivery.Clear();
            }
        }
        private void ExecuteStorage(ArrayList orderids)
        {
            DataSet goodsids=new DataSet();
            Dictionary<string, string> dic = new Dictionary<string, string>();
            int amount = 0;
            double  weight = 0.0f;
            int itemstorage = 0;
            double disstorage = 0.0f;
            int salemethod = 0;
            for (int i = 0; i < orderids.Count; i++)
            {
                goodsids = sql.SpecialItem("OrderDetail",
                             "OrderID=" + (string)orderids.ToArray().GetValue(i),
                             "GoodsID");
                for (int j = 0; j < goodsids.Tables[0].Rows.Count; j++)
                {
                    salemethod =Convert.ToInt32( sql.SingleSpecialItem("GoodsInfo", "ID=" + goodsids.Tables[0].Rows[j][0], "SaleMethod"));
                    if (salemethod < 2)         //整卖
                    {
                        itemstorage = Convert.ToInt32(sql.SingleSpecialItem("GoodsInfo",
                            "ID="+ goodsids.Tables[0].Rows[j][0],
                            "ItemStorage"));
                        amount = Convert.ToInt32(sql.SingleSpecialItem("OrderDetail",
                            "OrderID=" + (string)orderids.ToArray().GetValue(i) + " and GoodsID=" + goodsids.Tables[0].Rows[j][0],
                            "Amount"));
                        dic["ItemStorage"] = Convert.ToString(itemstorage - amount);
                        sql.UpdateMess("GoodsInfo", dic, "ID=" + goodsids.Tables[0].Rows[j][0]);

                    }
                    if (salemethod == 2)        //散卖
                    {
                        disstorage = Convert.ToDouble(sql.SingleSpecialItem("GoodsInfo",
                            "ID=" + goodsids.Tables[0].Rows[j][0],
                            "DisStorage"));
                        weight = Convert.ToDouble(sql.SingleSpecialItem("OrderDetail",
                            "OrderID=" + (string)orderids.ToArray().GetValue(i) + " and GoodsId=" + goodsids.Tables[0].Rows[j][0],
                            "Weight"));
                        dic["DisStorage"] = Convert.ToString(disstorage - weight);
                        sql.UpdateMess("GoodsInfo", dic, "ID=" + goodsids.Tables[0].Rows[j][0]);
                    }
                    dic.Clear();
                }
            }
        }

        protected void bind()
        {
            if (orderids==null )
            {
                Response.Write("<script>alert('无货可送')</script>");
                Response.Redirect("/Administration/UnfinishedOrders.aspx");
            }
            if(orderids.Count==0)
            {
                Response.Write("<script>alert('无货可送')</script>");
                Response.Redirect("/Administration/UnfinishedOrders.aspx");
            }

            /***************************************/
            
            //select A.ID,A.OrderID,B.GoodsName,A.Amount,A.Weight,A.Price,C.Area,D.CustomerName,D.Tel
            //from  OrderDetail A,GoodsInfo B,AddressInfo C,DeliveryInfo D
            //where (A.OrderID=orderids.ToArray(typeof(string)).GetValue(i)) and D.ID=delivery.ToArray(typeof(string)).GetValue(i))
            //and B.ID=A.GoodsID and C.ID=D.Address

            string constaint="(";
            for (int i = 0; i < orderids.ToArray().Length; i++)
            {
                if (i == (orderids.ToArray().Length-1))
                    constaint +=("(A.OrderID="+(string)orderids.ToArray(typeof(string)).GetValue(i)
                                    +" and "+
                                   "D.ID="+(string)delivery.ToArray(typeof(string)).GetValue(i)+")");
                else{
                    constaint +=("(A.OrderID="+(string)orderids.ToArray(typeof(string)).GetValue(i)
                                    +" and "+
                                   "D.ID="+(string)delivery.ToArray(typeof(string)).GetValue(i)+")"+" or ");
                } 
            }
            constaint += ") and B.ID=A.GoodsID and C.ID=D.Address";
           
            ds = sql.SpecialItem("OrderDetail A,GoodsInfo B,AddressInfo C,DeliveryInfo D",
                                  constaint,
                                  " A.ID,A.OrderID,B.GoodsName,A.Amount,A.Weight,A.Price,C.Area,D.CustomerName,D.Tel"
                                );

            GridView2.DataSource = ds;
            GridView2.DataKeyNames = new String[] { "ID" };
            GridView2.DataBind();
        }
    }
}